package cool.houge.pig.repository.register;

import cool.houge.pig.model.ActivationMail;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Mono;

/**
 * 帐户激活邮件数据仓库.
 *
 * @author KK (kzou227@qq.com)
 */
public interface ActivationMailRepository extends ReactiveCrudRepository<ActivationMail, Long> {

  /**
   * @param uid
   * @param captcha
   * @return
   */
  Mono<ActivationMail> findByUidAndCaptcha(long uid, String captcha);

  /**
   * 更新激活邮件的版本.
   *
   * @param id ID
   * @param ver 当前版本
   * @return 成功/失败
   */
  @Query(
      "UPDATE t_activation_mail SET ver=ver+1,update_time=current_timestamp() WHERE id=:id AND ver=:ver")
  @Modifying
  Mono<Void> updateVer(long id, int ver);
}
